//
//  TrafficRadioViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/11.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "TrafficRadioViewController.h"

#import "Track.h"

#import "DOUAudioStreamer.h"

static void *kStatusKVOKey = &kStatusKVOKey;

@interface TrafficRadioViewController ()
@property (strong, nonatomic) DOUAudioStreamer *streamer;
@property (strong, nonatomic) Track *track;
@end

@implementation TrafficRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    if (_streamer) {
        [_streamer pause];
    }
}

-(IBAction)playTrafficRadio:(id)sender
{
    if (_streamer) {
        
        if ([_streamer status] == DOUAudioStreamerPaused ||
            [_streamer status] == DOUAudioStreamerIdle) {
            [_streamer play];
        }
        else {
            [_streamer pause];
        }
        
    }else{
        _track = [Track new];
        _track.audioFileURL = [NSURL URLWithString:@"http://183.230.37.145:9909/video/admin/audio/downAudio"];
        
        [self resetStreamer];
    }
   
}

- (void)cancelStreamer
{
    if (_streamer != nil) {
        [_streamer pause];
        [_streamer removeObserver:self forKeyPath:@"status"];
        _streamer = nil;
    }
}

- (void)resetStreamer
{
    [self cancelStreamer];
    
    if (_track)
    {
        _streamer = [DOUAudioStreamer streamerWithAudioFile:_track];
        [_streamer addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:kStatusKVOKey];
        [_streamer play];
    }
}

- (void)updateStatus
{
    switch ([_streamer status]) {
        case DOUAudioStreamerPlaying:
            
            break;
            
        case DOUAudioStreamerPaused:
            
            break;
            
        case DOUAudioStreamerIdle:
            
            break;
            
        case DOUAudioStreamerFinished:
            
            break;
            
        case DOUAudioStreamerBuffering:
            
            break;
            
        case DOUAudioStreamerError:
            
            break;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == kStatusKVOKey) {
        [self performSelector:@selector(updateStatus)
                     onThread:[NSThread mainThread]
                   withObject:nil
                waitUntilDone:NO];
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}


-(void)dealloc
{
    [self cancelStreamer];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
